﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Globalization;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows;
using Microsoft.Win32;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Drawing.Text;
using System.IO;

namespace FaceCat {
    /// <summary>
    /// Gdi+绘图类
    /// </summary>
    public class WPFPaint : FCPaint{
        /// <summary>
        /// 位图
        /// </summary>
        public Bitmap m_bitmap;

        /// <summary>
        /// 画刷
        /// </summary>
        public System.Drawing.SolidBrush m_brush;

        /// <summary>
        /// 画刷的颜色
        /// </summary>
        public long m_brushColor;

        /// <summary>
        /// 绘图上下文
        /// </summary>
        public DrawingContext m_context;

        /// <summary>
        /// 空的字符串格式
        /// </summary>
        public System.Drawing.StringFormat m_emptyStringFormat;

        /// <summary>
        /// 导出路径
        /// </summary>
        public String m_exportPath;

        /// <summary>
        /// 绘图对象
        /// </summary>
        public System.Drawing.Graphics m_g;

        /// <summary>
        /// 是否裁剪
        /// </summary>
        private bool m_hasClip = false;

        /// <summary>
        /// 图像缓存
        /// </summary>
        public HashMap<String, Bitmap> m_images = new HashMap<String, Bitmap>();

        /// <summary>
        /// 图像矩阵
        /// </summary>
        public static float[][] m_matrixItems = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } };


         /// <summary>
        /// 横向偏移
        /// </summary>
        public int m_offsetX;

        /// <summary>
        /// 纵向偏移
        /// </summary>
        public int m_offsetY;

        /// <summary>
        /// 透明度
        /// </summary>
        public float m_opacity = 1;

        /// <summary>
        /// 绘图路径
        /// </summary>
        public GraphicsPath m_path;

        /// <summary>
        /// 画笔
        /// </summary>
        public System.Drawing.Pen m_pen;

        /// <summary>
        ///  画笔的颜色 
        /// </summary>
        public long m_penColor;

        /// <summary>
        /// 画笔的宽度 
        /// </summary>
        public float m_penWidth;

        /// <summary>
        ///  画笔的样式 
        /// </summary>
        public int m_penStyle;

        /// <summary>
        /// 资源路径
        /// </summary>
        public String m_resourcePath;

        /// <summary>
        /// 横向缩放因子
        /// </summary>
        public double m_scaleFactorX = 1;

        /// <summary>
        /// 纵向缩放因子
        /// </summary>
        public double m_scaleFactorY = 1;

        System.Drawing.BufferedGraphicsContext currentContext = System.Drawing.BufferedGraphicsManager.Current;
        public System.Drawing.BufferedGraphics m_myBuffer = null;

        public int m_oldWidth;

        public int m_oldHeight;

        public System.Drawing.Graphics m_inputG;

        public MyColor m_myColor = new MyColor();

        /// <summary>
        /// 缩放因子生效
        /// </summary>
        /// <param name="gdiplusRect">矩形</param>
        public void affectScaleFactor(ref System.Windows.Rect wpfRect)
        {
            if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
            {
                wpfRect.Location = new System.Windows.Point((int)(wpfRect.Left * m_scaleFactorX),
                    (int)(wpfRect.Top * m_scaleFactorY));
                wpfRect.Width = (int)(wpfRect.Width * m_scaleFactorX);
                wpfRect.Height = (int)(wpfRect.Height * m_scaleFactorY);
            }
        }


        /// <summary>
        /// 缩放因子生效
        /// </summary>
        /// <param name="gdiplusRect">矩形</param>
        public void affectGdiPlusScaleFactor(ref System.Drawing.Rectangle gdiplusRect)
        {
            if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
            {
                gdiplusRect.Location = new System.Drawing.Point((int)(gdiplusRect.Left * m_scaleFactorX),
                    (int)(gdiplusRect.Top * m_scaleFactorY));
                gdiplusRect.Width = (int)(gdiplusRect.Width * m_scaleFactorX);
                gdiplusRect.Height = (int)(gdiplusRect.Height * m_scaleFactorY);
            }
        }

        /// <summary>
        /// 添加曲线
        /// </summary>
        /// <param name="rect">矩形区域</param>
        /// <param name="startAngle">从 x 轴到弧线的起始点沿顺时针方向度量的角（以度为单位）</param>
        /// <param name="sweepAngle">从 startAngle 参数到弧线的结束点沿顺时针方向度量的角（以度为单位）</param>
        public override void addArc(FCRect rect, float startAngle, float sweepAngle)
        {
            if (m_g != null)
            {
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_path.AddArc(gdiPlusRect, startAngle, sweepAngle);
                }
            }
        }

        /// <summary>
        /// 添加贝赛尔曲线
        /// </summary>
        /// <param name="points">点阵</param>
        public override void addBezier(FCPoint[] points)
        {
            if (m_g != null)
            {
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                m_path.AddBezier(gdiPlusPoints[0], gdiPlusPoints[1], gdiPlusPoints[2], gdiPlusPoints[3]);
            }
        }

        /// <summary>
        /// 添加曲线
        /// </summary>
        /// <param name="points">点阵</param>
        public override void addCurve(FCPoint[] points)
        {
            if (m_g != null)
            {
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                m_path.AddCurve(gdiPlusPoints);
            }
        }

        /// <summary>
        /// 添加椭圆
        /// </summary>
        /// <param name="rect">矩形</param>
        public override void addEllipse(FCRect rect)
        {
            if (m_g != null)
            {
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_path.AddEllipse(gdiPlusRect);
                }
            }
        }

        /// <summary>
        /// 添加直线
        /// </summary>
        /// <param name="x1">第一个点的横坐标</param>
        /// <param name="y1">第一个点的纵坐标</param>
        /// <param name="x2">第二个点的横坐标</param>
        /// <param name="y2">第二个点的纵坐标</param>
        public override void addLine(int x1, int y1, int x2, int y2)
        {
            if (m_g != null)
            {
                int lx1 = x1 + m_offsetX;
                int ly1 = y1 + m_offsetY;
                int lx2 = x2 + m_offsetX;
                int ly2 = y2 + m_offsetY;
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    lx1 = (int)(m_scaleFactorX * lx1);
                    ly1 = (int)(m_scaleFactorY * ly1);
                    lx2 = (int)(m_scaleFactorX * lx2);
                    ly2 = (int)(m_scaleFactorY * ly2);
                }
                m_path.AddLine(lx1, ly1, lx2, ly2);
            }
        }

        /// <summary>
        /// 添加矩形
        /// </summary>
        /// <param name="rect">区域</param>
        public override void addRect(FCRect rect)
        {
            if (m_g != null)
            {
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_path.AddRectangle(gdiPlusRect);
                }
            }
        }

        /// <summary>
        /// 添加扇形
        /// </summary>
        /// <param name="rect">矩形区域</param>
        /// <param name="startAngle">从 x 轴到弧线的起始点沿顺时针方向度量的角（以度为单位）</param>
        /// <param name="sweepAngle">从 startAngle 参数到弧线的结束点沿顺时针方向度量的角（以度为单位）</param>
        public override void addPie(FCRect rect, float startAngle, float sweepAngle)
        {
            if (m_g != null)
            {
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_path.AddPie(gdiPlusRect, startAngle, sweepAngle);
                }
            }
        }

        /// <summary>
        /// 添加文字
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="font">字体</param>
        /// <param name="rect">区域</param>
        public override void addText(String text, FCFont font, FCRect rect, int width)
        {
            if (m_g != null)
            {
                if (m_emptyStringFormat == null)
                {
                    m_emptyStringFormat = StringFormat.GenericTypographic;
                    //m_emptyStringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
                }
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    int strX = (int)(m_scaleFactorX * (rect.left + m_offsetX));
                    int strY = (int)(m_scaleFactorY * (rect.top + m_offsetY));
                    float fontSize = (float)(font.m_fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
                    FCFont scaleFont = new FCFont(font.m_fontFamily, fontSize, font.m_bold, font.m_underline, font.m_italic);
                    Font gdiplusFont = getGdiPlusFont(scaleFont);
                    if (width == -1)
                    {
                        System.Drawing.Point gdiPlusPoint = new System.Drawing.Point(strX, strY);
                        m_path.AddString(text, gdiplusFont.FontFamily, (int)gdiplusFont.Style, gdiplusFont.Size, gdiPlusPoint, m_emptyStringFormat);
                    }
                    else
                    {
                        Rectangle gdiRect = new Rectangle(strX, strY,
                            (int)((rect.right - rect.left) * m_scaleFactorX),
                            (int)((rect.bottom - rect.top) * m_scaleFactorY));
                        if (gdiRect.Width > 0 && gdiRect.Height > 0)
                        {
                            m_path.AddString(text, gdiplusFont.FontFamily, (int)gdiplusFont.Style, gdiplusFont.Size, gdiRect, m_emptyStringFormat);
                        }
                    }
                }
                else
                {
                    Font gdiplusFont = getGdiPlusFont(font);
                    if (width == -1)
                    {
                        System.Drawing.Point gdiPlusPoint = new System.Drawing.Point(rect.left + m_offsetX, rect.top + m_offsetY);
                        m_path.AddString(text, gdiplusFont.FontFamily, (int)gdiplusFont.Style, gdiplusFont.Size, gdiPlusPoint, m_emptyStringFormat);
                    }
                    else
                    {
                        Rectangle gdiRect = new Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
                        if (gdiRect.Width > 0 && gdiRect.Height > 0)
                        {
                            m_path.AddString(text, gdiplusFont.FontFamily, (int)gdiplusFont.Style, gdiplusFont.Size, gdiRect, m_emptyStringFormat);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 开始导出
        /// </summary>
        /// <param name="exportPath">路径</param>
        /// <param name="rect">区域</param>
        public override void beginExport(String exportPath, FCRect rect)
        {
            m_exportPath = exportPath;
            int imageW = rect.right - rect.left;
            int imageH = rect.bottom - rect.top;
            if (imageW == 0) imageW = 1;
            if (imageH == 0) imageH = 1;
            if (m_g != null)
            {
                m_g.Dispose();
            }
            if (m_bitmap != null)
            {
                m_bitmap.Dispose();
            }
            m_bitmap = new Bitmap(imageW, imageH);
            m_g = Graphics.FromImage(m_bitmap);
            m_g.TextRenderingHint = TextRenderingHint.AntiAlias;
            m_opacity = 1;
            m_resourcePath = null;
        }

        /// <summary>
        /// 开始绘图
        /// </summary>
        public override void beginPaint(IntPtr hdc, FCRect wRect, FCRect pRect)
        {
            if (m_inputG != null)
            {
                int width = (int)(wRect.right - wRect.left);
                int height = (int)(wRect.bottom - wRect.top);
                if (m_myBuffer == null || width > m_oldWidth || height > m_oldHeight)
                {
                    if (m_g != null)
                    {
                        m_g.Dispose();
                    }
                    m_myBuffer = currentContext.Allocate(m_inputG, new System.Drawing.Rectangle(0, 0, width, height));
                    m_g = m_myBuffer.Graphics;
                    m_oldWidth = width;
                    m_oldHeight = height;
                }
                m_g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            }
            m_hasClip = false;
        }

        /// <summary>
        /// 开始一段路径
        /// </summary>
        public override void beginPath()
        {
            if (m_g != null)
            {
                m_path = new GraphicsPath();
            }
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        public override void clearCaches()
        {
            if (m_brush != null)
            {
                m_brush.Dispose();
                m_brush = null;
            }
            if (m_pen != null)
            {
                m_pen.Dispose();
                m_pen = null;
            }
            m_wpfPen = null;
            m_wpfBrush = null;
            if (m_emptyStringFormat != null)
            {
                m_emptyStringFormat.Dispose();
                m_emptyStringFormat = null;
            }
            foreach (Bitmap image in m_images.Values)
            {
                image.Dispose();
            }
            m_images.Clear();
            if (m_path != null)
            {
                m_path.Dispose();
                m_path = null;
            }
            if (m_bitmap != null)
            {
                m_bitmap.Dispose();
                m_bitmap = null;
            }
        }

        /// <summary>
        /// 裁剪路径
        /// </summary>
        public override void clipPath()
        {
            if (m_g != null)
            {
                m_g.SetClip(m_path);
            }
        }

        /// <summary>
        /// 闭合路径
        /// </summary>
        public override void closeFigure()
        {
            if (m_g != null)
            {
                m_path.CloseFigure();
            }
        }

        /// <summary>
        /// 结束一段路径
        /// </summary>
        public override void closePath()
        {
            if (m_g != null)
            {
                m_path.Dispose();
                m_path = null;
            }
        }

        /// <summary>
        /// 销毁对象
        /// </summary>
        public override void delete()
        {
            if (m_g != null)
            {
                m_g.Dispose();
                m_g = null;
            }
            if (m_bitmap != null)
            {
                m_bitmap.Dispose();
                m_bitmap = null;
            }
            clearCaches();
        }

        /// <summary>
        /// 绘制弧线
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="rect">矩形区域</param>
        /// <param name="startAngle">从 x 轴到弧线的起始点沿顺时针方向度量的角（以度为单位）</param>
        /// <param name="sweepAngle">从 startAngle 参数到弧线的结束点沿顺时针方向度量的角（以度为单位）</param>
        public override void drawArc(long dwPenColor, float width, int style, FCRect rect, float startAngle, float sweepAngle)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.DrawArc(getGdiPlusPen(dwPenColor, width, style), gdiPlusRect, startAngle, sweepAngle);
                }
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 设置贝赛尔曲线
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="points">坐标阵1</param>
        public override void drawBezier(long dwPenColor, float width, int style, FCPoint[] points)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                m_g.DrawBezier(getGdiPlusPen(dwPenColor, width, style), gdiPlusPoints[0], gdiPlusPoints[1], gdiPlusPoints[2], gdiPlusPoints[3]);
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 绘制曲线
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="points">坐标阵</param>
        public override void drawCurve(long dwPenColor, float width, int style, FCPoint[] points)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                m_g.DrawCurve(getGdiPlusPen(dwPenColor, width, style), gdiPlusPoints);
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="rect">矩形区域</param>
        public override void drawEllipse(long dwPenColor, float width, int style, FCRect rect)
        {
            if (dwPenColor == FCColor.None) return;
            int rw = rect.right - rect.left - 1;
            if (rw < 1) rw = 1;
            int rh = rect.bottom - rect.top - 1;
            if (rh < 1) rh = 1;
            if (m_g != null)
            {
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                m_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.DrawEllipse(getGdiPlusPen(dwPenColor, width, style), gdiPlusRect);
                }
                m_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            }
            else
            {
                System.Windows.Rect wpfRect = new System.Windows.Rect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectScaleFactor(ref wpfRect);
                if (wpfRect.Width > 0 && wpfRect.Height > 0)
                {
                    double rw2 = wpfRect.Right - wpfRect.Left - 1;
                    if (rw2 < 1) rw2 = 1;
                    double rh2 = wpfRect.Bottom - wpfRect.Top - 1;
                    if (rh2 < 1) rh2 = 1;
                    int centerX = (int)(wpfRect.Left + rw2 / 2);
                    int centerY = (int)(wpfRect.Top + rw2 / 2);
                    Geometry geoEllipse = new EllipseGeometry(new System.Windows.Point(centerX, centerY), rw2 / 2, rh2 / 2);
                    m_context.DrawGeometry(null, getPenWPF(dwPenColor, width, style), geoEllipse);
                }
            }
        }

        /// <summary>
        /// 绘制图片
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="rect">绘制区域</param>
        public override void drawImage(String imagePath, FCRect rect)
        {
            int dw = rect.right - rect.left, dh = rect.bottom - rect.top;
            String imageKey = m_resourcePath + imagePath + "," + dw.ToString() + "," + dh.ToString();
            int rw = rect.right - rect.left;
            if (rw < 1) rw = 1;
            int rh = rect.bottom - rect.top;
            if (rh < 1) rh = 1;
            Bitmap drawImage = null;
            System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
            if (m_images.ContainsKey(imageKey))
            {
                drawImage = m_images[imageKey];
            }
            else if (m_images.ContainsKey(imagePath))
            {
                drawImage = m_images[imagePath];
            }
            else
            {
                String[] attributes = new String[] { "file", "corner", "source", "highcolor", "lowcolor" };
                String[] values = new String[5];
                values[0] = imagePath;
                if (imagePath.IndexOf("=") != -1)
                {
                    for (int i = 0; i < attributes.Length; i++)
                    {
                        String attribute = attributes[i];
                        int alength = attribute.Length + 2;
                        int pos = imagePath.IndexOf(attribute + "=\'");
                        if (pos >= 0)
                        {
                            int rpos = imagePath.IndexOf("\'", pos + alength);
                            values[i] = imagePath.Substring(pos + alength, rpos - pos - alength);
                        }
                    }
                }
                String file = values[0];
                if (file.Length > 0)
                {
                    //fName = file.Substring(file.LastIndexOf("\\") + 1);
                }
                if (!File.Exists(file))
                {
                    if (m_resourcePath != null && m_resourcePath.Length > 0)
                    {
                        if (m_resourcePath.EndsWith("\\"))
                        {
                            file = m_resourcePath + file;
                        }
                        else
                        {
                            file = m_resourcePath + "\\" + file;
                        }
                    }
                }
                if (File.Exists(file))
                {
                    Bitmap image = null;
                    if (values[2] != null)
                    {
                        int[] source = new int[4];
                        String[] strs = values[2].Split(',');
                        for (int i = 0; i < strs.Length; i++)
                        {
                            source[i] = FCTran.strToInt(strs[i]);
                        }
                        Rectangle gdiPlusSourceRect = new Rectangle(source[0], source[1], source[2] - source[0], source[3] - source[1]);
                        Bitmap sourceImage = null;
                        try
                        {
                            sourceImage = (Bitmap)Bitmap.FromFile(file);
                        }
                        catch (Exception ex)
                        {
                        }
                        if (sourceImage != null)
                        {
                            image = new Bitmap(gdiPlusSourceRect.Width, gdiPlusSourceRect.Height);
                            Graphics sg = Graphics.FromImage(image);
                            sg.InterpolationMode = InterpolationMode.High;
                            sg.DrawImage(sourceImage, new Rectangle(0, 0, gdiPlusSourceRect.Width, gdiPlusSourceRect.Height), gdiPlusSourceRect, GraphicsUnit.Pixel);
                            sg.Dispose();
                            sourceImage.Dispose();
                        }
                    }
                    else
                    {
                        try
                        {
                            image = (Bitmap)Bitmap.FromFile(file);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    if (image != null)
                    {
                        long highColor = FCColor.None, lowColor = FCColor.None;
                        if (values[3] != null && values[4] != null)
                        {
                            highColor = FCTran.strToColor(values[3]);
                            lowColor = FCTran.strToColor(values[4]);
                        }
                        System.Drawing.Imaging.ImageAttributes imageAttr = new System.Drawing.Imaging.ImageAttributes();
                        if (highColor != FCColor.None && lowColor != FCColor.None)
                        {
                            int A = 0, R = 0, G = 0, B = 0;
                            FCColor.toRgba(this, highColor, ref R, ref G, ref B, ref A);
                            System.Drawing.Color gdiPlusHighColor = System.Drawing.Color.FromArgb(A, R, G, B);
                            FCColor.toRgba(this, lowColor, ref R, ref G, ref B, ref A);
                            System.Drawing.Color gdiPlusLowColor = System.Drawing.Color.FromArgb(A, R, G, B);
                            imageAttr.SetColorKey(gdiPlusLowColor, gdiPlusHighColor);
                        }
                        int iw = image.Width, ih = image.Height;
                        dw = (int)(dw * m_scaleFactorX);
                        dh = (int)(dh * m_scaleFactorY);
                        if (dw > iw)
                        {
                            dw = iw;
                        }
                        if (dh > ih)
                        {
                            dh = ih;
                        }
                        drawImage = new Bitmap(dw, dh);
                        Graphics g = Graphics.FromImage(drawImage);
                        //g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.InterpolationMode = InterpolationMode.High;
                        if (values[1] == null)
                        {
                            g.DrawImage(image, new Rectangle(0, 0, dw, dh),
                                0, 0, iw, ih, GraphicsUnit.Pixel, imageAttr);
                        }
                        else
                        {
                            int[] corners = new int[4];
                            String[] strs = values[1].Split(',');
                            for (int i = 0; i < strs.Length; i++)
                            {
                                corners[i] = FCTran.strToInt(strs[i]);
                            }
                            int left = 0;
                            int top = 0;
                            int right = iw;
                            int bottom = ih;
                            if (corners[0] > 0)
                            {
                                g.DrawImage(image, new Rectangle(left, top, corners[0], ih),
                                    0, 0, corners[0], ih, GraphicsUnit.Pixel, imageAttr);
                            }
                            if (corners[1] > 0)
                            {
                                g.DrawImage(image, new Rectangle(left, top, iw, corners[1]),
                                0, 0, iw, corners[1], GraphicsUnit.Pixel, imageAttr);
                            }
                            if (corners[2] > 0)
                            {
                                g.DrawImage(image, new Rectangle(right - corners[2], top, corners[2], ih),
                                    iw - corners[2], 0, corners[2], ih, GraphicsUnit.Pixel, imageAttr);
                            }
                            if (corners[3] > 0)
                            {
                                g.DrawImage(image, new Rectangle(left, bottom - corners[3], iw, corners[3]),
                                    0, ih - corners[3], iw, corners[3], GraphicsUnit.Pixel, imageAttr);
                            }
                            int cwdest = iw - corners[0] - corners[2];
                            int chdest = ih - corners[1] - corners[3];
                            int cwsrc = iw - corners[0] - corners[2];
                            int chsrc = ih - corners[1] - corners[3];
                            if (cwdest > 0 && chdest > 0 && cwsrc > 0 && chsrc > 0)
                            {
                                g.DrawImage(image, new Rectangle(left + corners[0], top + corners[1], cwdest, chdest),
                                corners[0], corners[1], cwsrc, chsrc, GraphicsUnit.Pixel, imageAttr);
                            }
                        }
                        imageAttr.Dispose();
                        g.Dispose();
                        image.Dispose();
                        m_images[imageKey] = drawImage;
                    }
                }
            }
            if (drawImage != null)
            {
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    //m_g.SmoothingMode = SmoothingMode.AntiAlias;
                    InterpolationMode oldMode = m_g.InterpolationMode;
                    //CompositingQuality oldQuality = m_g.CompositingQuality;
                    m_g.InterpolationMode = InterpolationMode.High;//高质量
                    //m_g.CompositingQuality = CompositingQuality.HighQuality;
                    if (m_opacity < 1)
                    {
                        m_matrixItems[3][3] = m_opacity;
                        System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(m_matrixItems);
                        System.Drawing.Imaging.ImageAttributes imageAttr = new System.Drawing.Imaging.ImageAttributes();
                        imageAttr.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
                        m_g.DrawImage(drawImage, gdiPlusRect, 0, 0, drawImage.Width, drawImage.Height, GraphicsUnit.Pixel, imageAttr);
                        imageAttr.Dispose();
                    }
                    else
                    {
                        m_g.DrawImage(drawImage, gdiPlusRect, 0, 0, drawImage.Width, drawImage.Height, GraphicsUnit.Pixel);
                    }
                    m_g.InterpolationMode = oldMode;
                }
                //m_g.InterpolationMode = oldMode;//高质量
                //m_g.CompositingQuality = oldQuality;
                //m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="left">左侧坐标</param>
        /// <param name="top">顶部左标</param>
        /// <param name="right">右侧坐标</param>
        /// <param name="bottom">底部坐标</param>
        public override void drawEllipse(long dwPenColor, float width, int style, int left, int top, int right, int bottom)
        {
            FCRect rect = new FCRect(left, top, right, bottom);
            drawEllipse(dwPenColor, width, style, rect);
        }

        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="x1">第一个点的横坐标</param>
        /// <param name="y1">第一个点的纵坐标</param>
        /// <param name="x2">第二个点的横坐标</param>
        /// <param name="y2">第二个点的纵坐标</param>
        public override void drawLine(long dwPenColor, float width, int style, int x1, int y1, int x2, int y2)
        {
            if (dwPenColor == FCColor.None) return;
            int lx1 = x1 + m_offsetX;
            int ly1 = y1 + m_offsetY;
            int lx2 = x2 + m_offsetX;
            int ly2 = y2 + m_offsetY;
            if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
            {
                lx1 = (int)(m_scaleFactorX * lx1);
                ly1 = (int)(m_scaleFactorY * ly1);
                lx2 = (int)(m_scaleFactorX * lx2);
                ly2 = (int)(m_scaleFactorY * ly2);
            }
            if (m_g != null)
            {
                m_g.DrawLine(getGdiPlusPen(dwPenColor, width, style), lx1, ly1, lx2, ly2);
            }
            else
            {
                m_context.DrawLine(getPenWPF(dwPenColor, width, style), new System.Windows.Point(lx1, ly1), new System.Windows.Point(lx2, ly2));
            }
        }

        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="x">第一个点的坐标</param>
        /// <param name="y">第二个点的坐标</param>
        public override void drawLine(long dwPenColor, float width, int style, FCPoint x, FCPoint y)
        {
            drawLine(dwPenColor, width, style, x.x, x.y, y.x, y.y);
        }

        /// <summary>
        /// 绘制路径
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        public override void drawPath(long dwPenColor, float width, int style)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                m_g.DrawPath(getGdiPlusPen(dwPenColor, width, style), m_path);
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 绘制扇形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="rect">矩形区域</param>
        /// <param name="startAngle">从 x 轴到弧线的起始点沿顺时针方向度量的角（以度为单位）</param>
        /// <param name="sweepAngle">从 startAngle 参数到弧线的结束点沿顺时针方向度量的角（以度为单位）</param>
        public override void drawPie(long dwPenColor, float width, int style, FCRect rect, float startAngle, float sweepAngle)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                Rectangle gdiPlusRect = new Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.DrawPie(getGdiPlusPen(dwPenColor, width, style), gdiPlusRect, startAngle, sweepAngle);
                }
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 绘制多边形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="points">点的数组</param>
        public override void drawPolygon(long dwPenColor, float width, int style, FCPoint[] points)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                m_g.DrawPolygon(getGdiPlusPen(dwPenColor, width, style), gdiPlusPoints);
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 绘制大量直线
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="points">点集</param>
        public override void drawPolyline(long dwPenColor, float width, int style, FCPoint[] points)
        {
            if (dwPenColor == FCColor.None) return;
            if (m_g != null)
            {
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                m_g.DrawLines(getGdiPlusPen(dwPenColor, width, style), gdiPlusPoints);
            }
            else
            {
                System.Windows.Point[] wpfPoints = new System.Windows.Point[points.Length];
                for (int i = 0; i < wpfPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Windows.Point p = new System.Windows.Point(x, y);
                    wpfPoints[i] = p;
                }
                PathGeometry pathGeo = new PathGeometry();
                PolyLineSegment polyline = new PolyLineSegment(wpfPoints, true);
                List<PolyLineSegment> segments = new List<PolyLineSegment>();
                segments.Add(polyline);
                PathFigure pathFigure = new PathFigure(wpfPoints[0], segments, false);
                pathGeo.Figures.Add(pathFigure);
                m_context.DrawGeometry(null, getPenWPF(dwPenColor, width, style), pathGeo);
            }
        }

        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="rect">矩形区域</param>
        public override void drawRect(long dwPenColor, float width, int style, FCRect rect)
        {
            if (dwPenColor == FCColor.None) return;
            int rw = rect.right - rect.left - 1;
            if (rw < 1) rw = 1;
            int rh = rect.bottom - rect.top - 1;
            if (rh < 1) rh = 1;
            if (m_g != null)
            {
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.DrawRectangle(getGdiPlusPen(dwPenColor, width, style), gdiPlusRect);
                }
            }
            else
            {
                System.Windows.Rect wpfRect = new System.Windows.Rect(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectScaleFactor(ref wpfRect);
                if (wpfRect.Width > 0 && wpfRect.Height > 0)
                {
                    Geometry geoRect = new RectangleGeometry(wpfRect);
                    m_context.DrawGeometry(null, getPenWPF(dwPenColor, width, style), geoRect);
                }
            }
        }

        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="left">左侧坐标</param>
        /// <param name="top">顶部左标</param>
        /// <param name="right">右侧坐标</param>
        /// <param name="bottom">底部坐标</param>
        public override void drawRect(long dwPenColor, float width, int style, int left, int top, int right, int bottom)
        {
            FCRect rect = new FCRect(left, top, right, bottom);
            drawRect(dwPenColor, width, style, rect);
        }

        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="font">字体</param>
        /// <param name="rect">矩形区域</param>
        public override void drawText(String text, long dwPenColor, FCFont font, FCRect rect, int width)
        {
            if (dwPenColor == FCColor.None) return;
            if (m_g != null)
            {
                if (m_emptyStringFormat == null)
                {
                    m_emptyStringFormat = System.Drawing.StringFormat.GenericTypographic;
                    //m_emptyStringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
                }
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    int strX = (int)(m_scaleFactorX * (rect.left + m_offsetX));
                    int strY = (int)(m_scaleFactorY * (rect.top + m_offsetY));
                    float fontSize = (float)(font.m_fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
                    FCFont scaleFont = new FCFont(font.m_fontFamily, fontSize, font.m_bold, font.m_underline, font.m_italic);
                    if (width == -1)
                    {
                        System.Drawing.Point gdiPlusPoint = new System.Drawing.Point(strX, strY);
                        m_g.DrawString(text, getGdiPlusFont(scaleFont), getGdiPlusBrush(dwPenColor), gdiPlusPoint, m_emptyStringFormat);
                    }
                    else
                    {
                        System.Drawing.Rectangle gdiRect = new System.Drawing.Rectangle(strX, strY,
                            (int)((rect.right - rect.left) * m_scaleFactorX),
                            (int)((rect.bottom - rect.top) * m_scaleFactorY));
                        if (gdiRect.Width > 0 && gdiRect.Height > 0)
                        {
                            m_g.DrawString(text, getGdiPlusFont(scaleFont), getGdiPlusBrush(dwPenColor), gdiRect, m_emptyStringFormat);
                        }
                    }
                }
                else
                {
                    if (width == -1)
                    {
                        System.Drawing.Point gdiPlusPoint = new System.Drawing.Point(rect.left + m_offsetX, rect.top + m_offsetY);
                        m_g.DrawString(text, getGdiPlusFont(font), getGdiPlusBrush(dwPenColor), gdiPlusPoint, m_emptyStringFormat);
                    }
                    else
                    {
                        System.Drawing.Rectangle gdiRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
                        m_g.DrawString(text, getGdiPlusFont(font), getGdiPlusBrush(dwPenColor), gdiRect, m_emptyStringFormat);
                    }
                }
            }
            else
            {
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    int strX = (int)(m_scaleFactorX * (rect.left + m_offsetX));
                    int strY = (int)(m_scaleFactorY * (rect.top + m_offsetY));
                    float fontSize = (float)(font.m_fontSize * (m_scaleFactorX + m_scaleFactorY) / 2);
                    FCFont scaleFont = new FCFont(font.m_fontFamily, fontSize, font.m_bold, font.m_underline, font.m_italic);
                    FormattedText ft = getFont(text, scaleFont, dwPenColor);
                    m_context.DrawText(ft, new System.Windows.Point(strX, strY));
                }
                else
                {
                    FormattedText ft = getFont(text, font, dwPenColor);
                    m_context.DrawText(ft, new System.Windows.Point(rect.left + m_offsetX, rect.top + m_offsetY));
                }
            }
        }

        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="font">字体</param>
        /// <param name="rect">矩形区域</param>
        public override void drawText(String text, long dwPenColor, FCFont font, FCRectF rect, int width)
        {
            drawText(text, dwPenColor, font, new FCRect(rect.left, rect.top, rect.right, rect.bottom), width);
        }

        /// <summary>
        /// 绘制圆角矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <param name="rect">矩形区域</param>
        /// <param name="cornerRadius">边角半径</param>
        public override void drawRoundRect(long dwPenColor, float width, int style, FCRect rect, int cornerRadius)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                Rectangle gdiPlusRect = new Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (cornerRadius != 0)
                {
                    GraphicsPath gdiPlusPath = getRoundRectPath(gdiPlusRect, (int)(cornerRadius * Math.Min(m_scaleFactorX, m_scaleFactorY)));
                    m_g.SmoothingMode = SmoothingMode.AntiAlias;
                    m_g.DrawPath(getGdiPlusPen(dwPenColor, width, style), gdiPlusPath);
                    m_g.SmoothingMode = SmoothingMode.Default;
                    gdiPlusPath.Dispose();
                }
                else
                {
                    if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                    {
                        m_g.SmoothingMode = SmoothingMode.Default;
                        m_g.DrawRectangle(getGdiPlusPen(dwPenColor, width, style), gdiPlusRect);
                    }
                }
            }
            else
            {
                drawRect(dwPenColor, width, style, rect);
            }
        }

        /// <summary>
        /// 绘制自动省略结尾的文字
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="font">字体</param>
        /// <param name="rect">矩形区域</param>
        public override void drawTextAutoEllipsis(String text, long dwPenColor, FCFont font, FCRect rect)
        {
            drawText(text, dwPenColor, font, rect);
        }

        /// <summary>
        /// 结束绘图
        /// </summary>
        public override void endPaint()
        {
            if (m_inputG != null)
            {
                if(m_myBuffer != null)
                {
                    m_myBuffer.Render();
                }
            }
        }

        /// <summary>
        /// 填充椭圆
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="rect">矩形区域</param>
        public override void fillEllipse(long dwPenColor, FCRect rect)
        {
            fillEllipse(dwPenColor, rect.left, rect.top, rect.right, rect.bottom);
        }

        /// <summary>
        /// 填充椭圆
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="left">左侧坐标</param>
        /// <param name="top">顶部左标</param>
        /// <param name="right">右侧坐标</param>
        /// <param name="bottom">底部坐标</param>
        public override void fillEllipse(long dwPenColor, int left, int top, int right, int bottom)
        {
            if (dwPenColor == FCColor.None) return;
            int rw = right - left;
            if (rw < 1) rw = 1;
            int rh = bottom - top;
            if (rh < 1) rh = 1;
            if (m_g != null)
            {
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(left + m_offsetX, top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    try
                    {
                        m_g.FillEllipse(getGdiPlusBrush(dwPenColor), gdiPlusRect);
                    }
                    catch (Exception ex)
                    {
                    }
                    m_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
                }
            }
            else
            {
                System.Windows.Rect wpfRect = new System.Windows.Rect(left + m_offsetX, top + m_offsetY, rw, rh);
                affectScaleFactor(ref wpfRect);
                if (wpfRect.Width > 0 && wpfRect.Height > 0)
                {
                    Geometry geoRect = new RectangleGeometry(wpfRect);
                    m_context.DrawGeometry(getBrushWPF(dwPenColor), null, geoRect);
                }
            }
        }

        /// <summary>
        /// 绘制渐变椭圆
        /// </summary>
        /// <param name="dwFirst">开始颜色</param>
        /// <param name="dwSecond">结束颜色</param>
        /// <param name="rect">矩形</param>
        /// <param name="angle">角度</param>
        public override void fillGradientEllipse(long dwFirst, long dwSecond, FCRect rect, int angle)
        {
            if (m_g != null)
            {
                int rw = rect.right - rect.left;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(gdiPlusRect, getGdiPlusColor(dwFirst), getGdiPlusColor(dwSecond), angle);
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.FillEllipse(lgb, gdiPlusRect);
                }
                m_g.SmoothingMode = SmoothingMode.Default;
                lgb.Dispose();
            }
        }

        /// <summary>
        /// 填充渐变路径
        /// </summary>
        /// <param name="dwFirst">开始颜色</param>
        /// <param name="dwSecond">结束颜色</param>
        /// <param name="points">点的集合</param>
        /// <param name="angle">角度</param>
        public override void fillGradientPath(long dwFirst, long dwSecond, FCRect rect, int angle)
        {
            if (m_g != null)
            {
                int rw = rect.right - rect.left;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(gdiPlusRect, getGdiPlusColor(dwFirst), getGdiPlusColor(dwSecond), angle);
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                m_g.FillPath(lgb, m_path);
                m_g.SmoothingMode = SmoothingMode.Default;
                lgb.Dispose();
            }
        }

        /// <summary>
        /// 绘制渐变的多边形
        /// </summary>
        /// <param name="dwFirst">开始颜色</param>
        /// <param name="dwSecond">结束颜色</param>
        /// <param name="rect">点的集合</param>
        /// <param name="angle">角度</param>
        public override void fillGradientPolygon(long dwFirst, long dwSecond, FCPoint[] points, int angle)
        {
            if (m_g != null)
            {
                int left = 0, top = 0, right = 0, bottom = 0;
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    if (i == 0)
                    {
                        left = x;
                        top = y;
                        right = x;
                        bottom = y;
                    }
                    else
                    {
                        if (x < left)
                        {
                            left = x;
                        }
                        if (y < top)
                        {
                            top = y;
                        }
                        if (x > right)
                        {
                            right = x;
                        }
                        if (y > bottom)
                        {
                            bottom = y;
                        }
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(left, top, right - left, bottom - top);
                if (gdiPlusRect.Height > 0)
                {
                    System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(gdiPlusRect, getGdiPlusColor(dwFirst), getGdiPlusColor(dwSecond), angle);
                    m_g.SmoothingMode = SmoothingMode.AntiAlias;
                    m_g.FillPolygon(lgb, gdiPlusPoints);
                    m_g.SmoothingMode = SmoothingMode.Default;
                    lgb.Dispose();
                }
            }
        }

        /// <summary>
        /// 绘制渐变矩形
        /// </summary>
        /// <param name="dwFirst">开始颜色</param>
        /// <param name="dwSecond">结束颜色</param>
        /// <param name="rect">矩形</param>
        /// <param name="cornerRadius">边角半径</param>
        /// <param name="angle">角度</param>
        public override void fillGradientRect(long dwFirst, long dwSecond, FCRect rect, int cornerRadius, int angle)
        {
            if (m_g != null)
            {
                int rw = rect.right - rect.left;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(gdiPlusRect, getGdiPlusColor(dwFirst), getGdiPlusColor(dwSecond), angle);
                if (cornerRadius != 0)
                {
                    GraphicsPath gdiPlusPath = getRoundRectPath(gdiPlusRect, (int)(cornerRadius * Math.Min(m_scaleFactorX, m_scaleFactorY)));
                    m_g.SmoothingMode = SmoothingMode.AntiAlias;
                    m_g.FillPath(lgb, gdiPlusPath);
                    m_g.SmoothingMode = SmoothingMode.Default;
                    gdiPlusPath.Dispose();
                }
                else
                {
                    if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                    {
                        m_g.SmoothingMode = SmoothingMode.Default;
                        m_g.FillRectangle(lgb, gdiPlusRect);
                    }
                }
                lgb.Dispose();
            }
        }

        /// <summary>
        /// 填充路径
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        public override void fillPath(long dwPenColor)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                m_g.FillPath(getGdiPlusBrush(dwPenColor), m_path);
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 绘制扇形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="rect">矩形区域</param>
        /// <param name="startAngle">从 x 轴到弧线的起始点沿顺时针方向度量的角（以度为单位）</param>
        /// <param name="sweepAngle">从 startAngle 参数到弧线的结束点沿顺时针方向度量的角（以度为单位）</param>
        public override void fillPie(long dwPenColor, FCRect rect, float startAngle, float sweepAngle)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                int rw = rect.right - rect.left - 1;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top - 1;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                m_g.SmoothingMode = SmoothingMode.AntiAlias;
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.FillPie(getGdiPlusBrush(dwPenColor), gdiPlusRect, startAngle, sweepAngle);
                }
                m_g.SmoothingMode = SmoothingMode.Default;
            }
        }

        /// <summary>
        /// 填充矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="left">左侧坐标</param>
        /// <param name="top">顶部左标</param>
        /// <param name="right">右侧坐标</param>
        /// <param name="bottom">底部坐标</param>
        public override void fillRect(long dwPenColor, int left, int top, int right, int bottom)
        {
            if (dwPenColor == FCColor.None) return;
            int rw = right - left;
            if (rw < 1) rw = 1;
            int rh = bottom - top;
            if (rh < 1) rh = 1;
            if (m_g != null)
            {
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(left + m_offsetX, top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                {
                    m_g.FillRectangle(getGdiPlusBrush(dwPenColor), gdiPlusRect);
                }
            }
            else
            {
                System.Windows.Rect wpfRect = new System.Windows.Rect(left + m_offsetX, top + m_offsetY, rw, rh);
                affectScaleFactor(ref wpfRect);
                if (wpfRect.Width > 0 && wpfRect.Height > 0)
                {
                    Geometry geoRect = new RectangleGeometry(wpfRect);
                    m_context.DrawGeometry(getBrushWPF(dwPenColor), null, geoRect);
                }
            }
        }

        /// <summary>
        /// 填充矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="rect">矩形区域</param>
        public override void fillRect(long dwPenColor, FCRect rect)
        {
            fillRect(dwPenColor, rect.left, rect.top, rect.right, rect.bottom);
        }

        /// <summary>
        /// 填充多边形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="points">点的数组</param>
        public override void fillPolygon(long dwPenColor, FCPoint[] points)
        {
            if (dwPenColor == FCColor.None) return;
            if (m_g != null)
            {
                System.Drawing.Point[] gdiPlusPoints = new System.Drawing.Point[points.Length];
                for (int i = 0; i < gdiPlusPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Drawing.Point p = new System.Drawing.Point(x, y);
                    gdiPlusPoints[i] = p;
                }
                m_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                m_g.FillPolygon(getGdiPlusBrush(dwPenColor), gdiPlusPoints);
                m_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            }
            else
            {
                System.Windows.Point[] wpfPoints = new System.Windows.Point[points.Length];
                for (int i = 0; i < wpfPoints.Length; i++)
                {
                    int x = points[i].x + m_offsetX;
                    int y = points[i].y + m_offsetY;
                    if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                    {
                        x = (int)(m_scaleFactorX * x);
                        y = (int)(m_scaleFactorY * y);
                    }
                    System.Windows.Point p = new System.Windows.Point(x, y);
                    wpfPoints[i] = p;
                }
                PathGeometry pathGeo = new PathGeometry();
                PolyLineSegment polyline = new PolyLineSegment(wpfPoints, true);
                List<PolyLineSegment> segments = new List<PolyLineSegment>();
                segments.Add(polyline);
                PathFigure pathFigure = new PathFigure(wpfPoints[0], segments, true);
                pathGeo.Figures.Add(pathFigure);
                m_context.DrawGeometry(getBrushWPF(dwPenColor), null, pathGeo);
            }
        }

        /// <summary>
        /// 填充圆角矩形
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="rect">矩形区域</param>
        /// <param name="cornerRadius">边角半径</param>
        public override void fillRoundRect(long dwPenColor, FCRect rect, int cornerRadius)
        {
            if (m_g != null)
            {
                if (dwPenColor == FCColor.None) return;
                int rw = rect.right - rect.left;
                if (rw < 1) rw = 1;
                int rh = rect.bottom - rect.top;
                if (rh < 1) rh = 1;
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rw, rh);
                affectGdiPlusScaleFactor(ref gdiPlusRect);
                if (cornerRadius != 0)
                {
                    GraphicsPath gdiPlusPath = getRoundRectPath(gdiPlusRect, (int)(cornerRadius * Math.Min(m_scaleFactorX, m_scaleFactorY)));
                    m_g.SmoothingMode = SmoothingMode.AntiAlias;
                    m_g.FillPath(getGdiPlusBrush(dwPenColor), gdiPlusPath);
                    m_g.SmoothingMode = SmoothingMode.Default;
                    gdiPlusPath.Dispose();
                }
                else
                {
                    if (gdiPlusRect.Width > 0 && gdiPlusRect.Height > 0)
                    {
                        m_g.SmoothingMode = SmoothingMode.Default;
                        m_g.FillRectangle(getGdiPlusBrush(dwPenColor), gdiPlusRect);
                    }
                }
            }
            else
            {
                fillRect(dwPenColor, rect.left, rect.top, rect.right, rect.bottom);
            }
        }

        /// <summary>
        /// 反裁剪路径
        /// </summary>
        public override void excludeClipPath()
        {
            if (m_g != null)
            {
                Region region = new Region(m_path);
                m_g.ExcludeClip(region);
                region.Dispose();
            }
        }

        /// <summary>
        /// 设置裁剪区域
        /// </summary>
        /// <param name="rect">区域</param>
        public virtual void excludeClip(FCRect rect)
        {
            if (m_g != null)
            {
                Rectangle gdiPlusRect = new Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    gdiPlusRect.X = (int)Math.Floor(gdiPlusRect.X * m_scaleFactorX);
                    gdiPlusRect.Y = (int)Math.Floor(gdiPlusRect.Y * m_scaleFactorY);
                    gdiPlusRect.Width = (int)Math.Ceiling(gdiPlusRect.Width * m_scaleFactorX);
                    gdiPlusRect.Height = (int)Math.Ceiling(gdiPlusRect.Height * m_scaleFactorY);
                }
                m_g.ExcludeClip(gdiPlusRect);
            }
        }

        /// <summary>
        /// 获取颜色
        /// </summary>
        /// <param name="dwPenColor">输入颜色</param>
        /// <returns>输出颜色</returns>
        public override long getColor(long dwPenColor)
        {
            long color = dwPenColor;
            if (dwPenColor < FCColor.None)
            {
                color = m_myColor.getUserColor(dwPenColor);
            }
            return color;
        }

        /// <summary>
        /// 获取要绘制的颜色
        /// </summary>
        /// <param name="dwPenColor">输入颜色</param>
        /// <returns>输出颜色</returns>
        public override long getPaintColor(long dwPenColor)
        {
            return getColor(dwPenColor);
        }

        /// <summary>
        /// 获取绘制圆形矩形的路径
        /// </summary>
        /// <param name="gdiPlusRect">矩形</param>
        /// <param name="cornerRadius">边角半径</param>
        /// <returns>路径</returns>
        public static GraphicsPath getRoundRectPath(Rectangle gdiPlusRect, int cornerRadius)
        {
            if (gdiPlusRect.Width > 1)
            {
                gdiPlusRect.Width -= 1;
            }
            int width = gdiPlusRect.Width, height = gdiPlusRect.Height;
            try
            {
                GraphicsPath gdiPlusPath = new GraphicsPath();
                gdiPlusPath.AddArc(gdiPlusRect.X, gdiPlusRect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
                gdiPlusPath.AddLine(gdiPlusRect.X + cornerRadius, gdiPlusRect.Y, gdiPlusRect.Right - cornerRadius * 2, gdiPlusRect.Y);
                gdiPlusPath.AddArc(gdiPlusRect.X + gdiPlusRect.Width - cornerRadius * 2, gdiPlusRect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
                gdiPlusPath.AddLine(gdiPlusRect.Right, gdiPlusRect.Y + cornerRadius * 2, gdiPlusRect.Right, gdiPlusRect.Y + gdiPlusRect.Height - cornerRadius * 2);
                gdiPlusPath.AddArc(gdiPlusRect.X + gdiPlusRect.Width - cornerRadius * 2, gdiPlusRect.Y + gdiPlusRect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
                gdiPlusPath.AddLine(gdiPlusRect.Right - cornerRadius * 2, gdiPlusRect.Bottom, gdiPlusRect.X + cornerRadius * 2, gdiPlusRect.Bottom);
                gdiPlusPath.AddArc(gdiPlusRect.X, gdiPlusRect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
                gdiPlusPath.AddLine(gdiPlusRect.X, gdiPlusRect.Bottom - cornerRadius * 2, gdiPlusRect.X, gdiPlusRect.Y + cornerRadius * 2);
                gdiPlusPath.CloseFigure();
                return gdiPlusPath;
            }
            catch (Exception ex)
            {
                GraphicsPath gdiPlusPath = new GraphicsPath();
                gdiPlusPath.AddRectangle(gdiPlusRect);
                return gdiPlusPath;
            }
        }

        /// <summary>
        /// 获取颜色
        /// </summary>
        /// <param name="dwPenColor">整型颜色</param>
        /// <returns>Gdi颜色</returns>
        public System.Windows.Media.Color getWPFColor(long dwPenColor)
        {
            long c = dwPenColor;
            if (dwPenColor < FCColor.None)
            {
                dwPenColor = getPaintColor(dwPenColor);
            }
            int a = 255;
            if (dwPenColor < 0)
            {
                dwPenColor = Math.Abs(dwPenColor);
                a = (int)(dwPenColor - dwPenColor / 1000 * 1000);
                dwPenColor /= 1000;
            }
            if (m_opacity < 1)
            {
                a = (int)(a * m_opacity);
            }
            int r = (int)(dwPenColor & 0xff);
            int g = (int)((dwPenColor >> 8) & 0xff);
            int b = (int)((dwPenColor >> 0x10) & 0xff);
            return System.Windows.Media.Color.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
        }

        public SolidColorBrush m_wpfBrush = null;

        /// <summary>
        /// 获取画刷
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <returns>画刷</returns>
        private SolidColorBrush getBrushWPF(long dwPenColor)
        {
            if (m_wpfBrush == null || m_brushColor != dwPenColor)
            {
                SolidColorBrush brush = new SolidColorBrush(getWPFColor(dwPenColor));
                m_wpfBrush = brush;
                m_brushColor = dwPenColor;
                return brush;
            }
            else
            {
                return m_wpfBrush;
            }
        }

        private System.Windows.Media.Pen m_wpfPen = null;

        /// <summary>
        /// 获取画笔
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <returns>画笔</returns>
        private System.Windows.Media.Pen getPenWPF(long dwPenColor, float width, int style)
        {
            System.Windows.Media.Pen pen = null;
            if (m_wpfPen == null)
            {
                float lineWidth = (float)(width * Math.Min(m_scaleFactorX, m_scaleFactorY));
                System.Windows.Media.Color wpfColor = getWPFColor(dwPenColor);
                System.Windows.Media.Brush brush = new SolidColorBrush(wpfColor);
                pen = new System.Windows.Media.Pen(brush, lineWidth);
                if (style == 0)
                {
                    pen.DashStyle = DashStyles.Solid;
                }
                else if (style == 1)
                {
                    pen.DashStyle = DashStyles.Dash;
                }
                else if (style == 2)
                {
                    pen.DashStyle = DashStyles.Dot;
                }
                m_wpfPen = pen;
                if (m_opacity == 1)
                {
                    m_penColor = dwPenColor;
                }
                else
                {
                    m_penColor = FCColor.None;
                }
                m_penWidth = lineWidth;
                m_penStyle = style;
            }
            else
            {
                float lineWidth = (float)(width * Math.Min(m_scaleFactorX, m_scaleFactorY));
                pen = m_wpfPen;
                if (m_penColor == FCColor.None || m_penColor != dwPenColor)
                {
                    System.Windows.Media.Color wpfColor = getWPFColor(dwPenColor);
                    System.Windows.Media.Brush brush = new SolidColorBrush(wpfColor);
                    pen = new System.Windows.Media.Pen(brush, lineWidth);
                    m_penColor = dwPenColor;
                    m_wpfPen = pen;
                    m_penWidth = lineWidth;
                }
                if (m_opacity != 1)
                {
                    m_penColor = FCColor.None;
                }
                if (m_penStyle != style)
                {
                    if (style == 0)
                    {
                        pen.DashStyle = DashStyles.Solid;
                    }
                    else if (style == 1)
                    {
                        pen.DashStyle = DashStyles.Dash;
                    }
                    else if (style == 2)
                    {
                        pen.DashStyle = DashStyles.Dot;
                    }
                    m_penStyle = style;
                }
                if (lineWidth != m_penWidth)
                {
                    pen.Thickness = lineWidth;
                    m_penWidth = lineWidth;
                }
            }
            return pen;
        }

        /// <summary>
        /// 获取格式化字体
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="font">字体</param>
        /// <param name="font">颜色</param>
        /// <returns>格式化字体</returns>
        private FormattedText getFont(string text, FCFont font, long dwPenColor)
        {
            FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new Typeface(font.m_fontFamily), font.m_fontSize, getBrushWPF(dwPenColor));
            if (font.m_bold)
            {
                ft.SetFontWeight(FontWeights.Bold);
            }
            if (font.m_italic)
            {
                ft.SetFontStyle(FontStyles.Italic);
            }
            if (font.m_underline)
            {
                ft.SetTextDecorations(TextDecorations.Underline);
            }
            return ft;
        }

        /// <summary>
        /// 获取颜色
        /// </summary>
        /// <param name="dwPenColor">整型颜色</param>
        /// <returns>Gdi颜色</returns>
        public System.Drawing.Color getGdiPlusColor(long dwPenColor)
        {
            long c = dwPenColor;
            if (dwPenColor < FCColor.None)
            {
                dwPenColor = getPaintColor(dwPenColor);
            }
            int a = 255;
            if (dwPenColor < 0)
            {
                dwPenColor = Math.Abs(dwPenColor);
                a = (int)(dwPenColor - dwPenColor / 1000 * 1000);
                dwPenColor /= 1000;
            }
            if (m_opacity < 1)
            {
                a = (int)(a * m_opacity);
            }
            int r = (int)(dwPenColor & 0xff);
            int g = (int)((dwPenColor >> 8) & 0xff);
            int b = (int)((dwPenColor >> 0x10) & 0xff);
            return System.Drawing.Color.FromArgb(a, r, g, b);
        }

        /// <summary>
        /// 获取Gdi字体
        /// </summary>
        /// <param name="font">字体</param>
        /// <returns>Gdi字体</returns>
        public System.Drawing.Font getGdiPlusFont(FCFont font)
        {
            if (font.m_fontSize < 1)
            {
                font.m_fontSize = 1;
            }
            float fontSize = font.m_fontSize;
            String fontFamily = font.m_fontFamily;
            if (fontFamily == "Default")
            {
                fontFamily = MyColor.getSystemFont();
            }
            System.Drawing.Font rFont = null;
            if (font.m_strikeout)
            {
                if (font.m_bold && font.m_underline && font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_bold && font.m_underline)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_bold && font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_underline && font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_bold)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_underline)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
                else
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Pixel);
                }
            }
            else
            {
                if (font.m_bold && font.m_underline && font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_bold && font.m_underline)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_bold && font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_underline && font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_bold)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_underline)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Pixel);
                }
                else if (font.m_italic)
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                }
                else
                {
                    rFont = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.GraphicsUnit.Pixel);
                }
            }
            return rFont;
        }

        /// <summary>
        /// 获取画笔
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <param name="width">宽度</param>
        /// <param name="style">样式</param>
        /// <returns>画笔</returns>
        public System.Drawing.Pen getGdiPlusPen(long dwPenColor, float width, int style)
        {
            System.Drawing.Color gdiColor = getGdiPlusColor(dwPenColor);
            if (m_pen == null)
            {
                float lineWidth = (float)(width * Math.Min(m_scaleFactorX, m_scaleFactorY));
                if (lineWidth < 1)
                {
                    lineWidth = 1;
                }
                m_pen = new System.Drawing.Pen(gdiColor, lineWidth);
                if (style == 0)
                {
                    m_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                }
                else if (style == 1)
                {
                    m_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                }
                else if (style == 2)
                {
                    m_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                }
                if (m_opacity == 1)
                {
                    m_penColor = dwPenColor;
                }
                else
                {
                    m_penColor = FCColor.None;
                }
                m_penWidth = lineWidth;
                m_penStyle = style;
            }
            else
            {
                float lineWidth = (float)(width * Math.Min(m_scaleFactorX, m_scaleFactorY));
                if (lineWidth < 1)
                {
                    lineWidth = 1;
                }
                if (m_penColor == FCColor.None || m_penColor != dwPenColor)
                {
                    m_pen.Color = gdiColor;
                    m_penColor = dwPenColor;
                }
                if (m_opacity != 1)
                {
                    m_penColor = FCColor.None;
                }
                if (m_penWidth != lineWidth)
                {
                    m_pen.Width = lineWidth;
                    m_penWidth = lineWidth;
                }
                if (m_penStyle != style)
                {
                    if (style == 0)
                    {
                        m_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                    }
                    else if (style == 1)
                    {
                        m_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                    }
                    else if (style == 2)
                    {
                        m_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                    }
                    m_penStyle = style;
                }
            }
            return m_pen;
        }

        /// <summary>
        /// 获取画刷
        /// </summary>
        /// <param name="dwPenColor">颜色</param>
        /// <returns>画刷</returns>
        public System.Drawing.SolidBrush getGdiPlusBrush(long dwPenColor)
        {
            System.Drawing.Color gdiColor = getGdiPlusColor(dwPenColor);
            if (m_brush == null)
            {
                m_brush = new System.Drawing.SolidBrush(gdiColor);
                if (m_opacity == 1)
                {
                    m_brushColor = dwPenColor;
                }
                else
                {
                    m_brushColor = FCColor.None;
                }
            }
            else
            {
                if (m_brushColor == FCColor.None || m_brushColor != dwPenColor)
                {
                    m_brush.Color = gdiColor;
                    m_brushColor = dwPenColor;
                }
                if (m_opacity != 1)
                {
                    m_brushColor = FCColor.None;
                }
            }
            return m_brush;
        }

        /// <summary>
        /// 获取偏移
        /// </summary>
        /// <returns>偏移坐标</returns>
        public override FCPoint getOffset()
        {
            return new FCPoint(m_offsetX, m_offsetY);
        }

        /// <summary>
        /// 获取缩放比例
        /// </summary>
        /// <param name="scaleFactorX">横向缩放比例</param>
        /// <param name="scaleFactorY">纵向缩放比例</param>
        public override void getScaleFactor(ref double scaleFactorX, ref double scaleFactorY)
        {
            scaleFactorX = m_scaleFactorX;
            scaleFactorY = m_scaleFactorY;
        }

        /// <summary>
        /// 旋转角度
        /// </summary>
        /// <param name="op">圆心坐标</param>
        /// <param name="mp">点的坐标</param>
        /// <param name="angle">角度</param>
        /// <returns>结果坐标</returns>
        public override FCPoint rotate(FCPoint op, FCPoint mp, int angle)
        {
            float PI = 3.14159265f;
            FCPoint pt = new FCPoint();
            pt.x = (int)((mp.x - op.x) * Math.Cos(angle * PI / 180) - (mp.y - op.y) * Math.Sin(angle * PI / 180) + op.x);
            pt.y = (int)((mp.x - op.x) * Math.Sin(angle * PI / 180) + (mp.y - op.y) * Math.Cos(angle * PI / 180) + op.y);
            return pt;
        }

        /// <summary>
        /// 设置裁剪区域
        /// </summary>
        /// <param name="rect">区域</param>
        public override void setClip(FCRect rect)
        {
            if (m_g != null)
            {
                System.Drawing.Rectangle gdiPlusRect = new System.Drawing.Rectangle(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    gdiPlusRect.X = (int)Math.Floor(gdiPlusRect.X * m_scaleFactorX);
                    gdiPlusRect.Y = (int)Math.Floor(gdiPlusRect.Y * m_scaleFactorY);
                    gdiPlusRect.Width = (int)Math.Ceiling(gdiPlusRect.Width * m_scaleFactorX);
                    gdiPlusRect.Height = (int)Math.Ceiling(gdiPlusRect.Height * m_scaleFactorY);
                }
                m_g.SetClip(gdiPlusRect);
            }
            else
            {
                System.Windows.Rect gdiPlusRect = new System.Windows.Rect(rect.left + m_offsetX, rect.top + m_offsetY, rect.right - rect.left, rect.bottom - rect.top);
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    gdiPlusRect.X = (int)Math.Floor(gdiPlusRect.X * m_scaleFactorX);
                    gdiPlusRect.Y = (int)Math.Floor(gdiPlusRect.Y * m_scaleFactorY);
                    gdiPlusRect.Width = (int)Math.Ceiling(gdiPlusRect.Width * m_scaleFactorX);
                    gdiPlusRect.Height = (int)Math.Ceiling(gdiPlusRect.Height * m_scaleFactorY);
                }
                if (m_hasClip)
                {
                    m_context.Pop();
                }
                System.Windows.Rect wpfRect = new System.Windows.Rect(gdiPlusRect.Left, gdiPlusRect.Top, gdiPlusRect.Width, gdiPlusRect.Height);
                RectangleGeometry rectGeo = new RectangleGeometry(wpfRect);
                m_context.PushClip(rectGeo);
                m_hasClip = true;
            }
        }

        /// <summary>
        /// 设置偏移
        /// </summary>
        /// <param name="mp">偏移坐标</param>
        public override void setOffset(FCPoint mp)
        {
            m_offsetX = mp.x;
            m_offsetY = mp.y;
        }

        /// <summary>
        /// 设置透明度
        /// </summary>
        /// <param name="opacity">透明度</param>
        public override void setOpacity(float opacity)
        {
            if (m_opacity != opacity)
            {
                m_opacity = opacity;
                if (m_brush != null)
                {
                    m_brush.Dispose();
                    m_brush = null;
                }
                if (m_pen != null)
                {
                    m_pen.Dispose();
                    m_pen = null;
                }
            }
        }

        /// <summary>
        /// 设置缩放因子
        /// </summary>
        /// <param name="scaleFactorX">横向因子</param>
        /// <param name="scaleFactorY">纵向因子</param>
        public override void setScaleFactor(double scaleFactorX, double scaleFactorY)
        {
            m_scaleFactorX = scaleFactorX;
            m_scaleFactorY = scaleFactorY;
        }

        /// <summary>
        /// 设置资源的路径
        /// </summary>
        /// <param name="resourcePath">资源的路径</param>
        public override void setResourcePath(String resourcePath)
        {
            m_resourcePath = resourcePath;
        }

        /// <summary>
        /// 设置是否支持透明色
        /// </summary>
        /// <returns>是否支持</returns>
        public override bool supportTransparent()
        {
            return true;
        }

         /// <summary>
        /// 获取文字大小
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="font">字体</param>z
        /// <returns>字体大小</returns>
        public override FCSize textSize(String text, FCFont font, int width) {
            FCSizeF sizeF = textSizeF(text, font, width);
            return new FCSize((int)sizeF.cx, (int)sizeF.cy);
        }

        /// <summary>
        /// 获取文字大小
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="font">字体</param>
        /// <returns>字体大小</returns>
        public override FCSizeF textSizeF(String text, FCFont font, int width)
        {
            if (m_g != null)
            {
                if (m_emptyStringFormat == null)
                {
                    m_emptyStringFormat = System.Drawing.StringFormat.GenericTypographic;
                    ////m_emptyStringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
                }
                int gWidth = Int32.MaxValue;
                if (width != -1)
                {
                    gWidth = width;
                }
                if (text == " ")
                {
                    m_emptyStringFormat.FormatFlags = System.Drawing.StringFormatFlags.MeasureTrailingSpaces;
                }
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    float rate = (float)((m_scaleFactorX + m_scaleFactorY) / 2);
                    float fontSize = (float)(font.m_fontSize * rate);
                    FCFont scaleFont = new FCFont(font.m_fontFamily, fontSize, font.m_bold, font.m_underline, font.m_italic);
                    System.Drawing.SizeF gdiPlusSize = m_g.MeasureString(text, getGdiPlusFont(scaleFont), (int)(gWidth * rate), m_emptyStringFormat);
                    if (text == " ")
                    {
                        m_emptyStringFormat.FormatFlags = System.Drawing.StringFormatFlags.FitBlackBox | System.Drawing.StringFormatFlags.LineLimit | System.Drawing.StringFormatFlags.NoClip;
                    }
                    return new FCSizeF(gdiPlusSize.Width / rate, gdiPlusSize.Height / rate);
                }
                else
                {
                    System.Drawing.SizeF gdiPlusSize = m_g.MeasureString(text, getGdiPlusFont(font), gWidth, m_emptyStringFormat);
                    if (text == " ")
                    {
                        m_emptyStringFormat.FormatFlags = System.Drawing.StringFormatFlags.FitBlackBox | System.Drawing.StringFormatFlags.LineLimit | System.Drawing.StringFormatFlags.NoClip;
                    }
                    return new FCSizeF(gdiPlusSize.Width, gdiPlusSize.Height);
                }
            }
            else
            {
                if (m_scaleFactorX != 1 || m_scaleFactorY != 1)
                {
                    float rate = (float)((m_scaleFactorX + m_scaleFactorY) / 2);
                    float fontSize = (float)(font.m_fontSize * rate);
                    FCFont scaleFont = new FCFont(font.m_fontFamily, fontSize, font.m_bold, font.m_underline, font.m_italic);
                    FormattedText ft = getFont(text, scaleFont, FCColor.None);
                    return new FCSizeF((float)ft.Width / rate, (float)ft.Height / rate);
                }
                else
                {
                    FormattedText ft = getFont(text, font, FCColor.None);
                    return new FCSizeF((float)ft.Width, (float)ft.Height);
                }
            }
        }
    }
}